/** sqlite数据库相关配置 */
import { pmProcessReportGetDetail }  from '@/api/comprehensive/comprehensive.js';
//巡检的所有接口
import * as XUNJIAN from '@/api/engineeringMaintenance/engineeringMaintenance.js'
import {getPicture} from '@/api/doc.js' //获取图片

/** 数据库名称 */
export const DATABASE_NAME = "city_escape";

/** 数据库路径 */
export const DATABASE_PATH = "_doc/city_escape.db";

/** 数据库表的名称 - 病害表 */
export const DISEASES_TABLE = "diseases";
/** 数据库表的名称 - 病害等级表(一级选项) */
export const TAB_DISEASEGRADE = "diseaseGrade";
/** 数据库表的名称 - 病害内容表(二级选项) */
export const TAB_DISEASECONTENT = "diseaseContent";
/** 数据库表的名称 - 日志表*/
export const TAB_DISEASEORDERLOG = "diseaseOrderLog";
/** 数据库表的名称 - 上次填报记录表*/
export const TAB_LASTFILLINGRECORD = "lastFillingRecord";
/** 数据库表的名称 - 关键字表*/
export const TAB_KEYWORDS = "keywordsTable";
/** 数据库表的名称 - 病害现状表*/
export const TAB_DISEASESTATUS = "diseaseStatus";
/** 数据库表的名称 - 修复措施表*/
export const TAB_REPAIR = "repairTable";
/** 数据库表的名称 - 选项表 */
export const OPTIONS_TABLE = "options";

/** 数据库表的名称 - 图片表 */
export const IMAGES_TABLE = "images";

/** get查询类型 */
export const OPT_GET = "get";

/** save查询类型 */
export const OPT_SAVE = "save";

/** update查询类型 */
export const OPT_UPDATE = "update";

/** delete查询类型 */
export const OPT_DELETE = "delete";


/** 离线缓存的选项数据对应的离线文件路径（或文件名） */
const _map = new Map();

// 病害查询
// _map.set(pmProcessReportGetDetail, {
// 	willSubmit: true, 
// 	order: 0, 
// 	type: OPT_GET, 
// 	// where: param => `docId=${param.docId}`,
// 	where: param => `1=1`,
// 	tableName: DISEASES_TABLE,
// 	children: [
// 		{ fieldName: "fileListImg", tableName: IMAGES_TABLE, fnCondition: parent => `_pid='${parent._id}'` }
// 	]
// });
// 病害保存
// _map.set(getPicture, {
// 	willSubmit: true, 
// 	order: 0, 
// 	type: OPT_SAVE, 
// 	fields: param => param, 
// 	tableName: DISEASES_TABLE, 
// 	children: [
// 		{ tableName: IMAGES_TABLE, fields: parent => parent.fileListImg },
// 	],
// });
// 病害删除
// _map.set(getPicture, {
// 	type: OPT_DELETE, 
// 	tableName: DISEASES_TABLE, 
// 	children: [
// 		{ tableName: IMAGES_TABLE, fnCondition: parent => `docId=${parent.docId}` },
// 	],
// });
// 修改
// _map.set(getPicture, {willSubmit: true, order: 0, type: OPT_UPDATE, fields: param => ({_id: "16782749419130526", id: "11", name: "张三"}), tableName: "test"});
// 测试...
_map.set(pmProcessReportGetDetail, {type: OPT_GET, order: 0, willSubmit: true, where: param => "1=1", tableName: "images"});
_map.set(XUNJIAN.diseaseOrderGetDetails, {
	type: OPT_GET,order: 1, willSubmit: false,
	single:true,
	where: param => `_id=${param.id}`, 
	tableName: DISEASES_TABLE, 
	children: [
		{fieldName:"diseaseOrderLogDto", tableName: TAB_DISEASEORDERLOG, fnCondition: parent => `_id=${parent._id}` },
	],
	resultMapping:(data)=>{
		return {
			...data, 
			isInfluenceEquipment:!!data.isInfluenceEquipment, 
			isProtectionSection:!!data.isProtectionSection, 
			isSandySoil:!!data.isSandySoil,
		}
	}
})
_map.set(XUNJIAN.diseaseOrderSave, { 
	type: OPT_SAVE, 
	fields: param => param, 
	exclude: ['diseaseOrderLogDto'], // 排除字段
	tableName: DISEASES_TABLE,
		children: [
			{tableName: TAB_DISEASEORDERLOG, fields: parent => parent.diseaseOrderLogDto },
		],
});
//获取列表数据
_map.set(XUNJIAN.diseaseOrderGetList, {
	type: OPT_GET, dataFieldName:'list', where: param => "1=1", tableName: DISEASES_TABLE ,
})

//获取所有详情列表
_map.set(XUNJIAN.getDetailsList, {
	type: OPT_GET,
	tableName: TAB_DISEASEORDERLOG,
	where: ({lineId, lineSiteSectionId, lineSiteSectionType}) => `1=1
		and lineId='${lineId}'
		and lineSiteSectionId='${lineSiteSectionId}'
		and lineSiteSectionType='${lineSiteSectionType}'
	`
})

//存储所有详情列表
_map.set(XUNJIAN.cacheDiseaseOrderLog, {
	type: OPT_SAVE, dataFieldName:'list', where: param => "1=1", tableName: TAB_DISEASEORDERLOG ,
})

//查询-病害等级(一级选项)
_map.set(XUNJIAN.diseaseGradeList, {
	type: OPT_GET, dataFieldName:'list', where: param => "1=1", tableName: TAB_DISEASEGRADE
})
//保存-病害等级(一级选项)
_map.set(XUNJIAN.cacheDiseaseGradeList, {
	type: OPT_SAVE, 
	tableName: TAB_DISEASEGRADE ,
	fields: param => param,
})
//查询-病害内容(二级)
_map.set(XUNJIAN.getDiseaseContent, {
	type: OPT_GET, tableName: TAB_DISEASECONTENT,
	where: ({diseaseGradeId}) => `diseaseGradeId=${diseaseGradeId}`,  
})
//保存-病害内容(二级)
_map.set(XUNJIAN.cacheDiseaseContent, {
	type: OPT_SAVE, 
	tableName: TAB_DISEASECONTENT,
	fields: param => param,
})

//更新状态接口(保存上次填报记录)
_map.set(XUNJIAN.inspectLogUpdateState, {
	type: OPT_SAVE,
	tableName: TAB_LASTFILLINGRECORD,
	fields: param => param,
})

//获取上次填报记录接口
_map.set(XUNJIAN.getLastFillingRecord, {
	type: OPT_GET, 
	single:true,
	where: param => "1=1",
	tableName: TAB_LASTFILLINGRECORD,
})



export const apiDataMapping = _map;

